package org.dromara.web.controller.sz.claim;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.system.domain.claim.ClaimMain;
import org.dromara.system.domain.claim.bo.ClaimMainBo;
import org.dromara.system.domain.claim.vo.ClaimImportVo;
import org.dromara.system.domain.claim.vo.ClaimMainVo;
import org.dromara.system.domain.policy.Policy;
import org.dromara.system.domain.policy.vo.PolicyImportVo;
import org.dromara.system.service.claim.IClaimMainService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 理赔主
 *
 * @author zj
 * @date 2025-11-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sz/main")
public class ClaimMainController extends BaseController {

    private final IClaimMainService claimMainService;

    /**
     * 查询理赔主列表
     */
    @GetMapping("/list")
    @SaIgnore
    public TableDataInfo<ClaimMainVo> list(ClaimMainBo bo, PageQuery pageQuery) {
        return claimMainService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出理赔主列表
     */
    @SaCheckPermission("system:main:export")
    @Log(title = "理赔主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ClaimMainBo bo, HttpServletResponse response) {
        List<ClaimMainVo> list = claimMainService.queryList(bo);
        ExcelUtil.exportExcel(list, "理赔主", ClaimMainVo.class, response);
    }

    /**
     * 获取理赔主详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:main:query")
    @GetMapping("/{id}")
    public R<ClaimMainVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(claimMainService.getInfoById(id));
    }

    /**
     * 新增理赔主
     */
    @SaCheckPermission("system:main:add")
    @Log(title = "理赔主", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ClaimMainBo bo) {
        return toAjax(claimMainService.insertByBo(bo));
    }

    /**
     * 修改理赔主
     */
    @SaCheckPermission("system:main:edit")
    @Log(title = "理赔主", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ClaimMainBo bo) {
        return toAjax(claimMainService.updateByBo(bo));
    }

    /**
     * 删除理赔主
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:main:remove")
    @Log(title = "理赔主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(claimMainService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 导入数据
     *
     * @param file 导入文件
     */
    @Log(title = "理赔单导入", businessType = BusinessType.IMPORT)
//    @SaCheckPermission("system:policy:import")
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @SaIgnore
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<ClaimImportVo> excelResult = ExcelUtil.importExcel(file.getInputStream(), ClaimImportVo.class, true);
        claimMainService.saveBatch(excelResult.getList());
        return R.ok(excelResult.getAnalysis());
    }
}
